//
//  PersonOrderListViewController.m
//  TestProject6
//
//  Created by JasonLee on 14/12/11.
//  Copyright (c) 2014年 JasonLee. All rights reserved.
//

#import "ProductListViewController.h"
#import "OrderModel.h"
#import "PersonOrderDetailViewController.h"
#import "ProtocolManager.h"
#import "Common.h"
#import "GoodDetailViewController.h"
#import "OrderViewController.h"
#import "TransforController.h"
#import "PullTableView.h"

@interface ProductListViewController ()<UITableViewDataSource, UITableViewDelegate, PullTableViewDelegate>

@property (nonatomic,assign) int currentIndex;

@property(nonatomic, strong)PullTableView *view_table;
@property(nonatomic, strong)NSMutableArray *array_data;

@end

@implementation ProductListViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    
    [self initView];
    self.currentIndex = -1;
    [self setEnableRefresh:NO];
    [self setEnableLoadMore:YES];
    [self getCommentList];
    [self.view setFrame:CGRectMake(0, 0, 320, [UIScreen mainScreen].bounds.size.height)];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (void)initView{
    UIView *view_navigationBar = [TransforController shardeInstanceOfNavigationBarAndStatusBarCoverViewWithY:0];
    [self.view addSubview:view_navigationBar];
    self.navigationItem.title = @"商品列表";
    self.view_table = [[PullTableView alloc]initWithFrame:CGRectMake(0, 65, 320, self.view.frame.size.height - HEIGHT_NAVIGATION_BAR) style:UITableViewStylePlain];
    self.view_table.dataSource = self;
    self.view_table.delegate = self;
    self.view_table.pullDelegate = self;
    self.view_table.tableFooterView = [[UIView alloc]init];
    
    [self.view addSubview:self.view_table];
    
    self.array_data = [[NSMutableArray alloc]init];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [self.array_data count];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = nil;
    NSArray *nibView = [[NSBundle mainBundle]loadNibNamed:@"SportsCell" owner:self options:nil];
    //    if([self.array_data count])
    NSDictionary *dic_temp = [NSDictionary dictionaryWithDictionary:[self.array_data objectAtIndex:indexPath.row]];
    cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"HotCommentsMiddle"];
    cell = [nibView objectAtIndex:1	];
    NSString *imgUrl = [Common valueForKey:@"photo" dic:dic_temp];
    UIImageView *img_head = [(UIImageView *)[cell viewWithTag:70010] retain];
    [img_head setImageWithURL:[Common getFullDownloadPathByRelativePath:imgUrl]];
    UILabel *lb_username = (UILabel *)[cell viewWithTag:70011];
    lb_username.text = [dic_temp objectForKey:@"name"];
    UILabel *lb_price = (UILabel *)[cell viewWithTag:70012];
    int type = [[Common valueForKey:@"type_id" dic:dic_temp] intValue];
    NSString *string_price = nil;
    if([[Common valueForKey:@"preferential_price" dic:dic_temp]floatValue] != 0){
        string_price = [Common valueForKey:@"price" dic:dic_temp];
    }else{
        string_price = [Common valueForKey:@"preferential_price" dic:dic_temp];
    }
    NSString *string_temp = nil;
    if(type == 10){
        string_temp = [NSString stringWithFormat:@"%@元 / 小时", string_price];
    }else{
        string_temp = [NSString stringWithFormat:@"%@元", string_price];
    }
    lb_price.text = string_temp;
    UIButton *btn_gotoBuy = (UIButton *)[cell viewWithTag:70013];
    NSString *payment_mode = [Common valueForKey:P_PAYMENT dic:dic_temp];
    if ([payment_mode isEqualToString:@"0"]) {
        btn_gotoBuy.backgroundColor = [Common colorWithRGB:0xf8b62d];
    } else {
        [btn_gotoBuy setTitle:@"立即预约" forState:UIControlStateNormal];
    }
    if([[Common valueForKey:@"is_sell" dic:dic_temp]isEqualToString:@"0"]){
        btn_gotoBuy.enabled = NO;
        btn_gotoBuy.backgroundColor = [UIColor grayColor];
    }
    [btn_gotoBuy setTag:6000000 + (indexPath.row)];
    [btn_gotoBuy addTarget:self action:@selector(gotoGoods:) forControlEvents:UIControlEventTouchUpInside];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    NSLog(@"%s || indexPath:%d", __FUNCTION__, indexPath.row);
    NSDictionary *dic_temp = [NSDictionary dictionaryWithDictionary:[self.array_data objectAtIndex:indexPath.row]];
    int target_id = [[Common valueForKey:@"id" dic:dic_temp] intValue];
    [self gotoGoodsWithId:target_id];
}

- (void)getCommentList{
    [[ProtocolManager shareInstance] getStoreListWithId:self.target_id andTarget:self selector:@selector(getCommentListCallBack:)];
}

- (void)getCommentListCallBack:(NSDictionary *)dic{
    NSLog(@"%s || dic:%@", __FUNCTION__, dic);
    if(dic == nil){
        [self resetPullTableViewState];
        return;
    }
    
    if([[Common valueForKey:@"respCode" dic:dic]isEqualToString:@"000"]){
//        self.array_data = [[NSMutableArray alloc]init];
        NSArray *array_temp = [NSMutableArray arrayWithArray:[Common valueForKey:@"goods" dic:dic]];
        for(int i = 0; i < array_temp.count; i++){
            [self.array_data addObject:array_temp[i]];
        }
        [self.view_table reloadData];
        if (array_temp.count < 10 ) {
            [self setEnableLoadMore:NO];
        }
        else {
            [self setEnableLoadMore:YES];
        }
        self.currentIndex += [array_temp count];
    }else{
        self.array_data = [[NSMutableArray alloc]init];
    }
    [self resetPullTableViewState];
}

- (void)gotoGoods:(id)sender{
    UIButton *btn = (UIButton *)sender;
    int target = btn.tag - 6000000;
    OrderViewController *orderViewCtrl = [[OrderViewController alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    NSDictionary *dic_temp = self.array_data[target];
    orderViewCtrl.storeId = [[Common valueForKey:@"id" dic:dic_temp]intValue];
    orderViewCtrl.goodsInfo = [Common valueForKey:@"name" dic:dic_temp];
    //    orderViewCtrl.storeInfo = self.storeInfo;
    //    NSDictionary *cellData = self.goodDetail;
    orderViewCtrl.product_name = [Common valueForKey:P_NAME dic:dic_temp];
    orderViewCtrl.product_price = [Common valueForKey:P_PRICE dic:dic_temp];
    orderViewCtrl.goodsId = [Common valueForKey:P_ID dic:dic_temp];
    orderViewCtrl.payment_mode = [Common valueForKey:P_PAYMENT dic:dic_temp];
    [self.navigationController pushViewController:orderViewCtrl animated:YES];
}

- (void)gotoGoodsWithId:(int)target_id{
    GoodDetailViewController *goodDetailViewController = [[GoodDetailViewController alloc]init];
    goodDetailViewController.goodId = target_id;
    goodDetailViewController.hidesBottomBarWhenPushed = YES;
    
    [self.navigationController pushViewController:goodDetailViewController animated:YES];
}



- (void)refreshTableView{
    NSLog(@"%s || actions'been actived.", __FUNCTION__);
    
    [self getCommentList];
}

#pragma mark - PullTableViewDelegate
- (void)pullTableViewDidTriggerRefresh:(PullTableView*)pullTableView
{
    NSLog(@"refresh...");
    self.currentIndex = -1;
    self.array_data = [[NSMutableArray alloc]init];
    [self getCommentList];
}

-(void)pullTableViewDidTriggerLoadMore:(PullTableView *)pullTableView
{
    NSLog(@"loadMore...");
    [self getCommentList];
}

- (void)resetPullTableViewState
{
    [self setPullTableIsRefreshing:NO];
    [self setPullTableIsLoadMore:NO];
}

- (void)setEnableRefresh:(BOOL)flag
{
    ((PullTableView*)self.view_table).enableRefresh = flag;
}

- (void)setEnableLoadMore:(BOOL)flag
{
    ((PullTableView*)self.view_table).enableLoadingMore = flag;
}

- (void)setPullRefreshDate:(NSDate *)date
{
    ((PullTableView*)self.view_table).pullLastRefreshDate = date;
}

- (void)setPullTableIsRefreshing:(BOOL)refreshing
{
    ((PullTableView*)self.view_table).pullTableIsRefreshing = refreshing;
}
- (void)setPullTableIsLoadMore:(BOOL)loadMore
{
    ((PullTableView*)self.view_table).pullTableIsLoadingMore = loadMore;
}

- (void)reloadRowIndexInSection:(int)section row:(int)row
{
    NSIndexPath *indexPath = [NSIndexPath indexPathForRow:row inSection:section];
    NSArray *indexPaths = [[NSArray alloc] initWithObjects:indexPath, nil];
    [self.view_table reloadRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationNone];
}

@end
